package com.pearlsoft.webapp.rms.resourcesMonitor;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import com.pearlsoft.rms.MetricResult;
import com.pearlsoft.rms.VcenterMonitor;
import com.pearlsoft.webapp.core.annotation.QueryHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Query;
import com.pearlsoft.webapp.core.dataset.Record;
import com.pearlsoft.webapp.core.dataset.SimpleRecord;
import com.pearlsoft.webapp.core.web.basehandler.AbsBaseQueryHandler;

@QueryHandlerComponent(name = "HistoryResourcesMonitor")
public class HistoryResourcesMonitor extends AbsBaseQueryHandler {

	/*
	 * 获取CPU历史信息
	 */
	public void getHistoryCpuInfo(Query query, DataSet ds) throws Exception {
		String vmname = (String) query.get("vmname");
		String counterName = (String) query.get("counterName");
		int starttime = Integer.parseInt((String) query.get("starttime"));
		int duration = Integer.parseInt((String) query.get("duration"));
		int interval = Integer.parseInt((String) query.get("interval"));
		VcenterMonitor vcm = this.getBean("vcenterMonitor",
				VcenterMonitor.class);
		MetricResult result = vcm.getHistoryCpuInfo(vmname,
				counterName, starttime, duration, interval);
		List<Record> list = new ArrayList<Record>();

		Calendar calStart = Calendar.getInstance();
		calStart.add(Calendar.MINUTE, -starttime);
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		if (!result.getData().isEmpty()) {
			for (Long listCpu : result.getData().get(0)) {
				SimpleRecord recrod = new SimpleRecord();
				String strDf = df.format(calStart.getTime());
				recrod.setValue("drawTime", strDf);
				recrod.setValue("infoPoint", listCpu);
				recrod.setValue("unit", result.getUnit());
				list.add(recrod);
				calStart.add(Calendar.SECOND, interval);
			}
		}
		ds.setRecords(list);
	}

	/*
	 * 获取内存历史信息
	 */
	public void getHistoryMemInfo(Query query, DataSet ds) throws Exception {
		String vmname = (String) query.get("vmname");
		String counterName = (String) query.get("counterName");
		int starttime = Integer.parseInt((String) query.get("starttime"));
		int duration = Integer.parseInt((String) query.get("duration"));
		int interval = Integer.parseInt((String) query.get("interval"));
		VcenterMonitor vcm = this.getBean("vcenterMonitor",
				VcenterMonitor.class);
		MetricResult result = vcm.getHistoryMemInfo(vmname,
				counterName, starttime, duration, interval);
		List<Record> list = new ArrayList<Record>();
		Calendar calStart = Calendar.getInstance();
		calStart.add(Calendar.MINUTE, -starttime);
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		if (!result.getData().isEmpty()) {
			for (Long listMem : result.getData().get(0)) {
				SimpleRecord recrod = new SimpleRecord();
				String strDf = df.format(calStart.getTime());
				recrod.setValue("drawTime", strDf);
				recrod.setValue("infoPoint", listMem);
				recrod.setValue("unit", result.getUnit());
				list.add(recrod);
				calStart.add(Calendar.SECOND, interval);
			}
		}
		ds.setRecords(list);
	};

	/*
	 * 获取硬盘历史信息
	 */
	public void getHistoryDiskInfo(Query query, DataSet ds) throws Exception {
		String vmname = (String) query.get("vmname");
		String counterName = (String) query.get("counterName");
		int starttime = Integer.parseInt((String) query.get("starttime"));
		int duration = Integer.parseInt((String) query.get("duration"));
		int interval = Integer.parseInt((String) query.get("interval"));
		VcenterMonitor vcm = this.getBean("vcenterMonitor",
				VcenterMonitor.class);
		MetricResult result = vcm.getHistoryDiskInfo(vmname,
				counterName, starttime, duration, interval);
		List<Record> list = new ArrayList<Record>();
		Calendar calStart = Calendar.getInstance();
		calStart.add(Calendar.MINUTE, -starttime);
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		if (!result.getData().isEmpty()) {
			for (Long listDisk : result.getData().get(0)) {
				SimpleRecord recrod = new SimpleRecord();
				String strDf = df.format(calStart.getTime());
				recrod.setValue("drawTime", strDf);
				recrod.setValue("infoPoint", listDisk);
				recrod.setValue("unit", result.getUnit());
				list.add(recrod);
				calStart.add(Calendar.SECOND, interval);
			}
		}
		ds.setRecords(list);
	}
	
	/*
	 * 获取硬盘历史信息
	 */
	public void getHistoryAllInfo(Query query, DataSet ds) throws Exception {
		String vmname = (String) query.get("vmname");
		String counterName = (String) query.get("counterName");
		int starttime = Integer.parseInt((String) query.get("starttime"));
		int duration = Integer.parseInt((String) query.get("duration"));
		int interval = Integer.parseInt((String) query.get("interval"));
		VcenterMonitor vcm = this.getBean("vcenterMonitor",
				VcenterMonitor.class);
		MetricResult memResult = vcm.getHistoryMemInfo(vmname,
				counterName, starttime, duration, interval);
		MetricResult cpuResult = vcm.getHistoryCpuInfo(vmname,
				counterName, starttime, duration, interval);
		MetricResult diskResult = vcm.getHistoryDiskInfo(vmname,
				counterName, starttime, duration, interval);
		List<Record> list = new ArrayList<Record>();
		Calendar calStart1 = Calendar.getInstance();
		Calendar calStart2 = Calendar.getInstance();
		Calendar calStart3 = Calendar.getInstance();
		calStart1.add(Calendar.MINUTE, -starttime);
		calStart2.add(Calendar.MINUTE, -starttime);
		calStart3.add(Calendar.MINUTE, -starttime);	
		SimpleDateFormat df = new SimpleDateFormat("HH:mm");
		if (!cpuResult.getData().isEmpty()) {
			for (Long listCpu : cpuResult.getData().get(0)) {
				SimpleRecord recrod = new SimpleRecord();
				String strDf = df.format(calStart1.getTime());
				recrod.setValue("type", "cpu");
				recrod.setValue("drawTime", strDf);
				recrod.setValue("infoPoint", listCpu);
				recrod.setValue("unit", cpuResult.getUnit());
				list.add(recrod);
				calStart1.add(Calendar.SECOND, interval);
			}
		}
		if (!memResult.getData().isEmpty()) {
			for (Long listMem : memResult.getData().get(0)) {
				SimpleRecord recrod = new SimpleRecord();
				String strDf = df.format(calStart2.getTime());
				recrod.setValue("type", "memory");
				recrod.setValue("drawTime", strDf);
				recrod.setValue("infoPoint", listMem);
				recrod.setValue("unit", memResult.getUnit());
				list.add(recrod);
				calStart2.add(Calendar.SECOND, interval);
			}
		}
		if (!diskResult.getData().isEmpty()) {
			for (Long listDisk : diskResult.getData().get(0)) {
				SimpleRecord recrod = new SimpleRecord();
				String strDf = df.format(calStart3.getTime());
				recrod.setValue("type", "disk");
				recrod.setValue("drawTime", strDf);
				recrod.setValue("infoPoint", listDisk);
				recrod.setValue("unit", diskResult.getUnit());
				list.add(recrod);
				calStart3.add(Calendar.SECOND, interval);
			}
		}
		ds.setRecords(list);
	}

	@Override
	public void advSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void getEntity(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void quickSearch(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void handler(Query query, DataSet ds) throws Exception {
		// TODO Auto-generated method stub

	}
}
